<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.png" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:onExit()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img id="photo" src="img/default.jpg" class="avtar" alt="">
                <h3 id="nickname">小可爱</h3>
                <a id="gitUrl" href="https://gitee.com/">git 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>0</span>
                    <span>0</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right" id="articleListDiv">
        </div>
    </div>
</body>

<script>
    
    function initList() {
        jQuery.ajax({
                url: "/article/mylist",
                type: "get",
                success: function(body) {
                    if(body.code == 200 && body.data != null && body.data.length > 0) {
                        //发表过文章
                        var html = ""
                        body.data.forEach(function(item) {
                            html += '<div class="blog">' +
                                        '<div class="title">' + item.title + '</div>' +
                                        ' <div class="date">' + item.createTime + '</div>' + 
                                        '<div class="desc">' + item.summary + '</div>' + 
                                        '<a href="blog_content.html?id=' + item.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;' + 
                                        '<a href="blog_edit.html?id=' + item.id + '" class="detail">编辑 &gt;&gt;</a>&nbsp;&nbsp;' +
                                        '<a href="#" class="detail delete" data-id="' + item.id + '">删除 &gt;&gt;</a>' +
                                    '</div>';
                            jQuery("#articleListDiv").html(html);

                            // 绑定删除事件
                            jQuery(".delete").click(function(event) {
                                event.preventDefault(); // 阻止默认行为（跳转）
                                var id = jQuery(this).data("id"); // 获取文章 id
                                if (confirm("确定要删除该文章吗？")) {
                                    // 发送 AJAX 请求删除文章
                                    jQuery.ajax({
                                        url: "/article/delete?id=" + id,
                                        type: "DELETE",
                                        success: function(body) {
                                            if(body.code == 200) {
                                                // 删除成功后刷新页面或者更新文章列表等操作
                                                alert("文章删除成功！");
                                                // 重新加载文章列表
                                                initList();
                                            } else {
                                                alert("文章删除失败！");
                                            }
                                        },
                                        error: function(err) {
                                            alert("登录状态过期，请重新登录");
                                            location.href = "/login.html";
                                        }
                                    });
                                }
                            });
                        })
                    } else { 
                        //未发表文章
                        jQuery("#articleListDiv").html("<h1>你还没有发过博客呢～</h1>");
                    }
                },
                error: function(err) {
                    alert("用户未登陆，即将跳转到登陆页！");
                    location.href = "/login.html";
                }
            });
    }
    initList();

    //获取个人信息
    function myInfo() {
        jQuery.ajax({
                url: "/user/myinfo",
                type: "get",
                success: function(body) {
                    if(body.code === 200 && body.data != null) {
                        //更改昵称
                        jQuery('#nickname').text(body.data.nickname);
                        if(body.data.photo !== '') {
                            jQuery('#photo').url(body.data.photo);
                        }
                        if(body.data.gitUrl !== '') {
                            jQuery('#gitUrl').href(body.data.gitUrl);
                        }
                    }
                },
                error: function(err) {
                    alert("用户未登陆，即将跳转到登陆页！");
                    location.href = "/login.html";
                }
            });
    }
    myInfo();

    //initList();
    function onExit() {
        if(confirm("确认退出？")) {
            jQuery.ajax({
                url: "/user/logout",
                type: "post",
                data: {},
                success: function(body) {
                    location.href = "/login.html";
                },
                error: function(err) {
                    alert("用户未登陆，即将跳转到登陆页！");
                    location.href = "/login.html";
                }
            });
        }
    }

    
</script>

</html>